
const db = wx.cloud.database();
// pages/applyOrder/applyOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      name: '',
      userID: '',
      userInfo: '',
      userIDImg: '',
      wx: '',
      phoneNumber: '',
      a:''
  },
 

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  toAgreement() {
      wx.navigateTo({
        url: '/pages/agreementS/agreementS',
      })
  },

  getAdminWX() {
    wx.showModal({
      confirmColor:'#00FF7F',	//确定的颜色
     /*  cancelColor:'#FF4040',  //取消的颜色 */
     showCancel: false,
      title:'点击复制微信号',
      content:'加急审核或福利群--请联系我-微信：YXHLYF19115314436',
      success(res) {
        if (res.confirm) {
            wx.setClipboardData({
                data: 'YXHLYF19115314436',
                success: function (res) {
                    wx.showToast({
                      title: '微信号已复制',
                      //icon:"none",
                      image:"/imgs/提交成功.png", //只能是本地的
                      mask:"true"//是否设置点击蒙版，防止点击穿透
                    })
              }
            })
        } else if (res.cancel) {
            console.log('用户点击取消')
        }
    }
    })
  },
 
  

/*   showTips() {
      wx.showModal({
        title: '常见问题',
        showCancel: false,
        content: '1、上传学生证带有 姓名+学号 那页截图即可 其他信息可打码 2、加急申请请联系微信客服 3、说明：我们会尊重您的隐私，请放心^~~^'
      })
  }, */

  uploadImg() {
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed', 'original'],
        sourceType: ['album', 'camera'],
        success: (res) => {
            wx.showLoading({
              title: '加载中',
            })
            const random = Math.floor(Math.random() * 1000);
            wx.cloud.uploadFile({
                cloudPath: `userIDImg/${this.data.userInfo.nickName}-${random}.png`,
                filePath: res.tempFilePaths[0],
                success: (res) => {
                    let fileID = res.fileID;
                    this.setData({
                        userIDImg: fileID,
                    })
                    wx.hideLoading()
                }
            })
        }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
/*       const userInfo = wx.getStorageSync('userInfo');
      console.log(userInfo)
      this.setData({
          userInfo,  */
                  // 过审核

        const that = this
        db.collection("a").get()
          .then(res => {
              console.log("11111111",res.data[0].a)
            that.setData({
              a: res.data[0].a,
            })
          })
        

 
/* let videoAd = null

 
if (wx.createRewardedVideoAd) {
  videoAd = wx.createRewardedVideoAd({
    adUnitId: 'adunit-0e9522e17c86f2a0'
  })
  videoAd.onLoad(() => {})
  videoAd.onError((err) => {})
  videoAd.onClose((res) => {})
}

 
if (videoAd) {
  videoAd.show().catch(() => {
   
    videoAd.load()
      .then(() => videoAd.show())
      .catch(err => {
        console.log('激励视频 广告显示失败')
      })
  })
}
 */

 


    
      try {
        var user = wx.getStorageSync('key')
        if (user) {
          this.setData({
            userInfo: user
          })
        }
      } catch (e) {
        // Do something when catch error
      }
  },

  /* 获取姓名 */
getname(e) {
  this.setData({
    name: e.detail.value
  })
},

  /* 获取学号 */
  getuserID(e) {
    this.setData({
      userID: e.detail.value
    })
  },
  getuserPhone(e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },

    /* 获取微信号 */
    getwx(e) {
      this.setData({
        wx: e.detail.value
      })
    },

  /* 提交 */
  submit() {
    // 保存this指向，方便复用
    const that = this.data;
    // 判断
    if (!that.name || !that.userID || !that.userIDImg || !that.wx || !that.phoneNumber) {
      wx.showToast({
        icon:"error",
        title: '信息不全',
      })
      return;
  } 
    // 提交信息
    db.collection('orderReceive').add({
        data: {
            name: that.name,
            userID: that.userID,
            phoneNumber: that.phoneNumber,
            userIDImg: that.userIDImg,
            wx: that.wx,
            userInfo: that.userInfo,
            state: '待审核',
        },
        success: (res) => {
            // 清空输入内容
            this.setData({
                name: '',
                userID: '',
                wx: '',
                userIDImg: '',
            })
            wx.showToast({
              title: '提交成功',
            })
            wx.navigateTo({
              url: '../receiveLoading/receiveLoading',
            })
        },
        fail: (res) => {
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
        }
    })
},
 
})